<template>
  <div>
    <page-list :x-uri="uri" :custom-components="custom" :doactions="doactions">
      <!-- 自定义tab -->
      <template v-slot:page-tabs>
        <edu-page-tabs :tab-list="xTabs" />
        <page-header :content="headerTitle" />
        <title-agent :row="options" />
      </template>
      <!-- 页面操作按钮 -->
      <template v-slot:page-button>
        <el-button v-auth="'course.agent.list.info'" size="mini" type="primary" @click="create">添加机构</el-button>
      </template>
    </page-list>

    <!-- 课程同步弹出层 -->
    <sync-video v-if="syncVideoVisible" :visible.sync="syncVideoVisible" />
  </div>
</template>
<script>
import PageList from '@public/template/page_list.vue';
import School from './components/title_school.vue';
import TitleAgent from './components/title_info.vue';
import SyncVideo from './components/sync_video.vue';
import Price from './components/price.vue';
import CostPrice from './components/cost_price.vue';
import EduPageTabs from '@public/template/tabs/page_tabs.vue';
import PageHeader from '@/components/page-header';

export default {
  components: {
    PageList,
    TitleAgent,
    SyncVideo,
    EduPageTabs,
    PageHeader
  },
  inject: ['reload'],
  data() {
    return {
      uri: 'course/admin/agent/' + this.$route.params.id + '/info',
      headerTitle: '详情',
      xTabs: [
        {
          title: '课程代理',
          uri: '/course/agent/' + this.$route.params.id + '/info',
          meta: {
            permission: 'course.agent.list.info'
          }
        }
      ],
      custom: {
        school: School,
        price: Price,
        cost_price: CostPrice
      },
      options: {
        cover_url: '',
        title: '',
        price: '0.00',
        cost_price: '0.00'
      },
      syncVideoVisible: false, // 弹出层
      doactions: [
        {
          title: '删除',
          meta: { permission: 'course.agent.list.info' },
          callback: this.delete
        }
      ]
    };
  },
  created() {
    this.loadOptions();
  },
  methods: {
    loadOptions() {
      this.$http.post(
          '/course/admin/agent/' + this.$route.params.id + '/videoinfo',
          {
            course_id: this.$route.params.id
          },
          response => {
            this.options = response.data;
          },
          _ => {},
          false
      );
    },
    // 课程同步
    create() {
      this.syncVideoVisible = true;
    },
    // 删除
    delete: function({ row }) {
      this.$box
          .confirm('确认删除吗')
          .then(() => {
            this.$http.delete(
                '/course/admin/agent/' + row.id + '/delete',
                {},
                response => {
                  this.$message.success(response.msg, _ => {
                    this.reload();
                  });
                },
                error => {
                  this.$message.error(error.msg);
                }
            );
          })
          .catch(() => {});
    }
  }
};
</script>
